package com.jp.app.server.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.jp.app.server.model.Book;
import com.jp.app.server.model.Result;
import com.jp.app.server.services.BookService;
 
@RestController
@RequestMapping("/book")
public class BookController {
 
    @Autowired
    protected BookService service;
 
    @RequestMapping(value = "/getBooks.json", method = RequestMethod.GET)
    public @ResponseBody Result getBooks(@RequestParam(required = false) String title) {
		Result result = new Result();
    	List<Book> books = service.getBooks();
    	result.setRecords(books);
		return result;
    }
 
    @RequestMapping(value = "/create-book.json", method = RequestMethod.POST)
    public String createBookPost(@ModelAttribute("book") Book book) {
        service.createBook(book);
        return "redirect:books";
    }
}